package com.feinno.module.security.domain;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.OrderBy;
import org.hibernate.annotations.Parameter;
import org.hibernate.validator.constraints.NotBlank;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.feinno.framework.common.domain.AbstractEntity;

@Entity
@Table(name = "SYS_MENU")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnoreProperties(value = { "res"}, ignoreUnknown = true)
public class Menu extends AbstractEntity {
	/** UID */
	private static final long serialVersionUID = 8091566583669606667L;

	/** 名称 */
	private String name;

	/** 描述 */
	private String descn;

	/** 图标 */
	private String icon;

	/** 排序时间(默认倒序) */
	private Date orderTime = new Date();

	/** 是否显示(0:显示,1:隐藏) */
	private int showState = 0;

	/** 所含资源 */
	private Set<Resource> res;

	@Column(name = "DESCN")
	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	@Id
	@GeneratedValue(generator = "sequence")
	@GenericGenerator(name = "sequence", strategy = "sequence", parameters = { @Parameter(name = "sequence", value = "SEQ_SYS_MENU") })
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@NotBlank
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@OneToMany(mappedBy = "menu", targetEntity = Resource.class)
	@Cascade(value = { CascadeType.SAVE_UPDATE })
	@OrderBy(clause = "id")
	@LazyCollection(value = LazyCollectionOption.EXTRA)
	public Set<Resource> getRes() {
		return res;
	}

	public void setRes(Set<Resource> res) {
		this.res = res;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Date getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(Date orderTime) {
		this.orderTime = orderTime;
	}

	public int getShowState() {
		return showState;
	}

	public void setShowState(int showState) {
		this.showState = showState;
	}

	@Override
	public String toString() {
		return String.format("Menu: {name:%s, descn:%s}", name, descn);
	}

}
